<!--
 * @Author: Aaron
 * @Date: 2021-12-27 17:49:53
 * @LastEditors: YanMeng
 * @LastEditTime: 2022-11-01 09:32:34
 * @Description: file content
 * @FilePath: \ias_ui\src\views\coi\components\categoryAttributes.vue
 * @Company: 沃尔玛
-->
<template>
  <el-row v-if="categoryAttributes.length" :gutter="gutter" style="width: 100%">
    <el-col v-if="attName" :span="24" class="head">{{ attName }}</el-col>
    <el-col v-for="(item, index) in categoryAttributes" :key="index" :span="span" :xl="xl">
      <el-form-item
        v-if="item.inputType == 1 || item.inputType == 2 || item.inputType == 3"
        :label="item.attrName"
        :prop="`${keyName}[${findAttrIndex(item.attrId)}].attrValue`"
        :rules="{ required: item.requiredFlag, message: `${item.attrName}不能为空`, trigger: 'blur' }"
      >
        <el-input
          v-if="item.inputType == 3"
          v-model="commodityAttribute[keyName][findAttrIndex(item.attrId)].attrValue"
          :placeholder="item | getPlaceholder"
          :disabled="isDisabled"
        >
          <span v-if="item.units" slot="suffix" class="suffix">{{ item.units | getString }}</span>
        </el-input>
        <el-popover v-if="item.inputType == 3" placement="bottom" trigger="click">
          <img :src="item.graphic" alt="指引图片" />
          <i v-if="item.graphic" slot="reference" class="el-icon-question" />
        </el-popover>
        <el-select
          v-if="item.inputType == 1 || item.inputType == 2"
          v-model="commodityAttribute[keyName][findAttrIndex(item.attrId)].attrValue"
          value-key="attrValueId"
          clearable
          :placeholder="`请选择${item.attrName}`"
          :multiple="item.inputType == 2"
          :disabled="isDisabled"
        >
          <el-option v-for="op in item.attrValueList" :key="op.attrValueId" :label="op.attrValueName" :value="op.attrValueId" />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="item.inputType == 10"
        :label="item.attrName"
        class="unit"
        :prop="`${keyName}[${findAttrIndex(item.attrId)}]`"
        :rules="{ required: item.requiredFlag, validator: validator, trigger: 'blur' }"
      >
        <el-form-item v-for="(unitItem, unitIndex) in setMultiUnit(item)" :key="unitIndex" class="analytical-unit" :label="item.expands[unitIndex] || ''">
          <el-input v-model="commodityAttribute[keyName][findAttrIndex(item.attrId)].attrValue[unitIndex]" :disabled="isDisabled" clearable />
          <el-select
            v-model="commodityAttribute[keyName][findAttrIndex(item.attrId)].unitValue[unitIndex]"
            placeholder=" "
            :disabled="isDisabled"
            clearable
            @change="change()"
          >
            <el-option v-for="(unit, unit_Index) in unitItem" :key="unit_Index" :label="unit" :value="unit" />
          </el-select>
        </el-form-item>
      </el-form-item>
      <el-form-item
        v-if="item.inputType == 11"
        class="addr"
        :label="item.attrName"
        :prop="`${keyName}[${findAttrIndex(item.attrId)}]`"
        :rules="{ required: item.requiredFlag, validator: regionValidator, trigger: 'blur' }"
      >
        <el-radio-group
          v-if="Array.isArray(commodityAttribute[keyName][findAttrIndex(item.attrId)].attrValue)"
          v-model="commodityAttribute[keyName][findAttrIndex(item.attrId)].attrValue[0]"
          :disabled="isDisabled"
          @change="addrChange(item, 0)"
        >
          <el-radio :label="0">国内</el-radio>
          <el-radio :label="1">海外</el-radio>
        </el-radio-group>

        <el-select
          v-for="(addr, addrIndex) in getAddr(item)"
          :key="addr"
          v-model="commodityAttribute[keyName][findAttrIndex(item.attrId)].attrValue[addrIndex + 1]"
          class="select"
          placeholder=" "
          :disabled="isDisabled"
          clearable
          @change="addrChange(item, addrIndex + 1)"
        >
          <el-option
            v-for="(unit, unit_Index) in addrOptions[getOptionsKey(item.attrValue, addrIndex)]"
            :key="unit_Index"
            :label="unit.areaName"
            :value="unit.areaId"
          />
        </el-select>
      </el-form-item>
    </el-col>
  </el-row>
</template>

<script>
import { jdArea } from '@/api/coi/onlineTaskAPI/sku.js'

export default {
  name: 'CategoryAttributes',
  filters: {
    getString(v) {
      var string = ''
      function getSon(data) {
        if (Array.isArray(data)) getSon(data[0])
        else {
          string = data
          return
        }
      }
      getSon(v)
      return string
    },
    getPlaceholder(item) {
      return item.maintainRemark ? item.maintainRemark : `请输入${item.attrName}`
    }
  },
  props: {
    commodityAttribute: {
      type: Object,
      default: () => {
        return {}
      }
    },
    categoryAttributes: {
      type: Array,
      default() {
        return []
      }
    },
    isDisabled: {
      type: Boolean,
      default: true
    },
    attName: {
      type: String,
      default: ''
    },
    span: {
      type: Number,
      default: 12
    },
    xl: {
      type: Number,
      default: 8
    },
    gutter: {
      type: Number,
      default: 0
    },
    keyName: {
      type: String,
      default: 'categoryAttributeList'
    }
  },
  data() {
    return {
      validator: (rule, value, callback) => {
        if (!value.requiredFlag || (value.attrValue.every((_) => _) && value.unitValue.every((_) => _))) callback()
        else callback(new Error('信息填写不完整'))
      },
      regionValidator: (rule, value, callback) => {
        if (!value.requiredFlag || value.attrValue.every((_) => typeof _ === 'number')) callback()
        else callback(new Error('信息填写不完整'))
      },
      addrOptions: {}
    }
  },
  methods: {
    findAttrIndex(id) {
      return this.commodityAttribute[this.keyName].findIndex((item) => item.attrId === id)
    },
    change() {
      this.$forceUpdate()
    },
    set(arr) {
      return [...new Set(arr)]
    },
    getAddr(item) {
      if (Array.isArray(item.attrValue) && item.attrValue.length === item.inputAreaLevel - 0 + 1) {
        const arr = item.attrValue.filter((_) => _ || typeof _ === 'number')
        if (arr.length <= item.inputAreaLevel) return arr.length
        else return item.inputAreaLevel - 0
      } else {
        if (Array.isArray(item.attrValue)) {
          item.attrValue.length = item.inputAreaLevel - 0 + 1
        } else {
          item.attrValue = new Array(item.inputAreaLevel - 0 + 1).fill(null)
        }
        return this.getAddr(item)
      }
    },
    getOptionsKey(value, index) {
      const key = `${value[0]}-${value[index]}`
      if (!this.addrOptions[key]) {
        const params = {}
        if (index === 0) params.internalFlag = value[index]
        else {
          params.internalFlag = value[0]
          params.parentId = value[index]
        }
        jdArea(params).then(({ result }) => {
          if (result) this.$set(this.addrOptions, key, result)
        })
      }
      return key
    },
    addrChange(item, index) {
      let i = index
      while (i < item.attrValue.length) {
        i += 1
        if (item.attrValue[i]) item.attrValue[i] = null
      }
      this.$forceUpdate()
    },
    setMultiUnit(item) {
      if (Array.isArray(item.units)) {
        if (!item.attrValue || !Array.isArray(item.attrValue) || item.attrValue.length !== item.units.length) {
          item.attrValue = new Array(item.units.length).fill('')
        }
        if (!item.unitValue || !Array.isArray(item.unitValue) || item.unitValue.length !== item.units.length) {
          item.unitValue = new Array(item.units.length).fill('')
        }
      }
      return item.units
    }
  }
}
</script>

<style lang="scss" scoped>
.analytical-unit {
  margin: 0;
  /deep/ .el-form-item__label {
    width: auto !important;
    &::before {
      content: '';
      margin: 0;
    }
  }
  /deep/ .el-form-item__content {
    margin-left: 0 !important;
  }
}
/deep/ .unit > .el-form-item__content {
  flex-wrap: wrap;
}
.addr {
  /deep/ .el-radio-group {
    display: flex;
    flex-direction: column;
  }
  .el-radio {
    margin-right: 10px;
  }
  .select {
    margin-left: 5px;
  }
}
.el-icon-question {
  margin-left: 10px;
  cursor: pointer;
  font-size: 16px;
}
/deep/ .el-form-item__content {
  display: flex;
  align-items: center;
}
.suffix {
  margin-right: 10px;
}
/deep/ .el-radio {
  line-height: 28px;
}
</style>
